import { HTTP } from '../../../utils/http.js'
let ajax = new HTTP()
var app = getApp()

Page({
    data: {
        imgUrls: [],
        currentTab: 0,
        winHeight: "",
        id: 1,
        tabTitleData: [],
        tabContentArr: [],
        cur: 0,
        placeType: "vision",
        cateid: ""
    },
    onLoad(options) {
        let memberid = app.globalData.memberid
        this._getSwiperList(memberid)
        this._gethList(memberid)
        this.getSystemInfo();
    },
    // 转发 
    onShareAppMessage: function () {

    },
    //滑动
    getSystemInfo() {
        wx.getSystemInfo({
            success: (res) => {
                let clientHeight = res.windowHeight
                let clienWidth = res.windowWidth
                let rpxR = 750 / clienWidth
                let calc = clientHeight * rpxR
                console.log(calc)
                this.setData({
                    winHeight: calc - 618
                })
            },
        })
    },
    // 点击列表
    switchNav(event) {
        let cur = event.currentTarget.dataset.index
        let cateid = event.currentTarget.dataset.current
        if (this.data.currentTab == cur) {
            return false
        } else {
            this.setData({
                currentTab: cur,
                cateid: cateid
            })
            wx.showNavigationBarLoading()
            this._getshuList(app.globalData.memberid, this.data.cateid, cur)
        }
    },
    // 左右滑动
    switchTab(event) {
        let cateid = this.data.tabTitleData[event.detail.current].id
        this.setData({
            currentTab: event.detail.current,
            cateid: cateid,
            cur: event.detail.current
        })
        wx.showNavigationBarLoading()
        this._getshuList(app.globalData.memberid, this.data.cateid, event.detail.current)
    },
    // 点击轮播
    onSwiperTap(event) {
        let id = event.detail.id
        let title = event.detail.title
        wx.navigateTo({
            url: 'detail/detail?id=' + id + '&title=' + title
        })
    },
    // 搜索跳转
    onSearchTap(event) {
        wx.navigateTo({
            url: '/pages/search/search?id=' + this.data.placeType,
        })
    },
    // 跳转内页
    onVideoTap(event) {
        console.log(event)
        let postId = event.currentTarget.dataset.postid
        let postTitle = event.currentTarget.dataset.posttitle
        wx.navigateTo({
            url: 'detail/detail?id=' + postId + '&title=' + postTitle,
        })
    },
    // 请求轮播数据
    _getSwiperList(memberid) {
        ajax.request({
            url: "videolist/banner",
            data: {
                memberid: memberid
            },
            success: (res) => {
                this.setData({
                    imgUrls: res.data
                })
            }
        })
    },
    // 请求视频分类
    _gethList(memberid) {
        ajax.request({
            url: "videocate",
            data: {
                memberid: memberid
            },
            success: (res) => {
                if (res.rescode === "0000") {
                    for (let i = 0; i < res.data.length; i++) {
                        this.data.tabContentArr.push([])
                    }
                    this.setData({
                        tabTitleData: res.data,
                        cateid: res.data[0].id,
                        cur: 0,
                        tabContentArr: this.data.tabContentArr
                    })
                    wx.showNavigationBarLoading()
                    this._getshuList(memberid, this.data.cateid, this.data.cur)
                }
            }
        })
    },
    // 请求对应分类下视频的数据
    _getshuList(memberid, cateid, cur) {
        ajax.request({
            url: "videolist",
            data: {
                cateid: cateid,
                memberid: memberid
            },
            success: (res) => {
                wx.hideNavigationBarLoading()
                this.data.tabContentArr[cur] = res.data
                this.setData({
                    tabContentArr: this.data.tabContentArr
                })
            }
        })
    }
})